package com.vay.de.dang.online.web;

import com.vay.de.dang.online.resp.RRepaymentAccountDto;
import com.vay.de.dang.online.resp.RRepaymentAll;
import com.vay.de.dang.online.resp.RRepaymentInfo;
import com.vay.de.dang.online.util.VnUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class BillApi extends BaseApi {

    public static void queryBill(int billType, int pageIndex, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    Map<String, Object> map = new HashMap<>();
                    map.put("pageNo", pageIndex);
                    map.put("pageSize", 10);
                    map.put("state", billType);
                    String param = JsonUtil.objectToJson(map);
                    //String url = wrapperUrl("loanSupermarket/my/getApplyList");
                    String url = wrapperUrl("fhVKEA/SjBmpWAq/IyzizvWL");
                    String json = doPostByJson(url, param);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toRApplyList(json));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 还款信息（还款金额，还款方式）
    public static void repaymentAllInfo(String applyNo, ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {

                    RRepaymentAll all = new RRepaymentAll();
                    // 还款金额
                    //String path = "loanSupermarket/payRepayment/repaymentManageInfo?applyNo=" + applyNo;
                    // 【applyNo->gAIp】
                    String path = "JCaar/JfVZrDiJ/jiBpMo?gAIp=" + applyNo;
                    String url = wrapperUrl(path);
                    String json = doPostByBody(url);
                    if (JsonUtil.isOk(json)) {
                        all.setInfo(JsonUtil.toObjectByData(json, RRepaymentInfo.class));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }

                    // 全部还款方式
                    //path = "loanSupermarket/payRepayment/repaymentInfo/all?applyNo=" + applyNo;
                    //applyNo->rBV
                    path = "HjI/QFRAuWAC/eYJ/NfFMt?rBV=" + applyNo;
                    url = wrapperUrl(path);
                    json = doPostByBody(url);
                    if (JsonUtil.isOk(json)) {
                        all.setWays(JsonUtil.toAllRepaymentWay(json));
                        return ReqResult.ok(all);
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 上传还款凭证
    public static void repaymentCommit(float amount, String applyNo,
                                       String channel, String type, String path,
                                       ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    Map<String, Object> map = new HashMap<>();
                    map.put("amount", amount);
                    map.put("applyNo", applyNo);

                    String pu = syncUploadFile(path);
                    if (VnUtil.isEmpty(pu)) {
                        return ReqResult.error("Voucher Path is null");
                    }

                    map.put("passbookUrls", Arrays.asList(pu));
                    map.put("repaymentChannel", channel);
                    map.put("verificationType", type);

                    // # 167. 上传还款凭证
                    //【/loanSupermarket/payRepayment/passbook/upload——>/gdym/IPVjnG/VpBWAg/UaDDmNi】
                    String path = "gdym/IPVjnG/VpBWAg/UaDDmNi";
                    String url = wrapperUrl(path);
                    String json = doPostByJson(url, JsonUtil.objectToJson(map));
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toObjectByData(json, RRepaymentAccountDto.class));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

}
